import sys
from pathlib import Path

file = Path(__file__).resolve()
parent, top = file.parent, file.parents[2]

sys.path.append(str(top))
try:
    sys.path.remove(str(parent))
except ValueError: # Already removed
    pass

__package__ = 'src.model'

from ..utils.ids import datetime_8
from sqlalchemy import Column, String, BigInteger, CHAR, DateTime, Integer, Numeric
from ..utils.database import Base, mes_engine


class DetectHistory(Base):
    __tablename__ = 'DetectHistory'
    __table_args__ = {'comment': '检查结果', 'mysql_charset': 'utf8mb4', 'extend_existing': True}

    ID = Column(BigInteger, primary_key=True, autoincrement=True, comment='Lot ID')
    ChipID = Column(String(16), nullable=False, comment='CHIP ID')
    LotNo = Column(String(16), comment='Lot No')
    ModuleNo = Column(String(20), comment='机种')
    UserID = Column(String(9), comment='作業者Code')
    RecipeName = Column(String(20), comment='Recipe name')
    ResultName = Column(String(2), comment='Result Name OK/NG/GR')
    ResultCode = Column(Integer, nullable=False, comment='Result Code OK=1/NG=2/Gray=3')
    ErrorCodeJdi = Column(String(10), comment='JDI Code 再検査は手入力')
    ErrorCodeHyc = Column(Integer, comment='不良种类 分類ー再検査時はNull')
    EquipNo = Column(String(8), comment='检查机号')
    DetectionNo = Column(Integer, comment='検査窓口No')
    JigNo = Column(String(4), comment='冶具No')
    RetestCode = Column(Integer, comment='再検査Code')
    DetectionTime = Column(DateTime, nullable=False, default=datetime_8, comment='检查时间')
    DetectionPattern = Column(String(20), comment='检查画面')
    DefectNo = Column(Integer, nullable=False, comment='不良No, 按照发现缺陷的顺序对其进行编号。对服务器上存储的缺陷总数设置了最大限制。最多输出 10 个。')
    DefectCount = Column(Integer, comment='不良総数')
    Defect_X = Column(Numeric(18, 4), comment='不良座標X1')
    Defect_Y = Column(Numeric(18, 4), comment='不良座標Y1')
    Defect_W = Column(Numeric(18, 4), comment='幅W')
    Defect_H = Column(Numeric(18, 4), comment='高さH')
    Defect_Dust1 = Column(Numeric(18, 4), comment='灰尘1')
    Defect_Dust2 = Column(Numeric(18, 4), comment='灰尘2')
    Defect_Brightness = Column(Numeric(18, 4), comment='亮度1')
    Defect_Min_Area = Column(Numeric(18, 4), comment='最小面积')
    Freedom_Vcom = Column(Numeric(18, 4), comment='VCOM値')
    Freedom_Current = Column(Numeric(18, 4), comment='電流値')
    Freedom_Flk = Column(Numeric(18, 4), comment='FLK値')
    CassetCode = Column(String(20), comment='Casset-code')
    Defect_Reserve1 = Column(Numeric(18, 4), comment='备用1')
    Defect_Reserve2 = Column(Numeric(18, 4), comment='备用2')
    Defect_Reserve3 = Column(Numeric(18, 4), comment='备用3')
    Defect_Reserve4 = Column(Numeric(18, 4), comment='备用4')
    Defect_Reserve5 = Column(Numeric(18, 4), comment='备用5')
    Defect_Reserve6 = Column(Numeric(18, 4), comment='备用6')
    Defect_Reserve7 = Column(Numeric(18, 4), comment='备用7')
    Defect_Reserve8 = Column(Numeric(18, 4), comment='备用8')
    Defect_Reserve9 = Column(Numeric(18, 4), comment='备用9')
    Defect_Reserve10 = Column(Numeric(18, 4), comment='备用10')
    Freedom_ErrorCode = Column(String(50), comment='Freedom 错误代码')
    Freedom_Info = Column(String(500), comment='Freedom 检查详情')
    FetchedFlag = Column(Integer, default=1, comment='MES读取前flag=1, 读取后flag=2')
    

class simulatorresult(Base):
    __tablename__ = 'simulatorresult'
    __table_args__ = {'comment': 'NG情報', 'mysql_charset': 'utf8mb4', 'extend_existing': True}

    ID = Column(BigInteger, primary_key=True, autoincrement=True, comment='Lot ID')
    ChipID = Column(String(16), nullable=False, comment='CHIP ID')
    RetestCode = Column(Integer, comment='再検査Code')
    UptDate = Column(DateTime, default=datetime_8, comment='更新日時')


if __name__ == "__main__":
    # 创建表
    Base.metadata.create_all(bind=mes_engine)